package cn.gok.studentsystem.controller;

import cn.gok.studentsystem.entity.Student;
import cn.gok.studentsystem.service.StudentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/student")
public class StudentController {
    //1）	查询所有学生信息
    //2）	根据ID查询学生信息
    //3）	模糊查询学生信息
    //4）	分页查询学生信息
    //5）	添加学生信息
    //6）	修改学生信息
    //7）	根据ID删除学生
    //8）	批量删除学生
    @Autowired
    private StudentService studentService;
    //查询所有学生信息
    @RequestMapping("/selectAllStudents")
    public List<Student> selectAllStudents(){
        return studentService.list(null);
    }
    //根据ID查询学生信息
    @RequestMapping("/selectStudentById")
    public Student selectStudentById(Long id){
        return studentService.getById(id);
    }
    //模糊查询学生信息
    @RequestMapping("/selectStudentMh")
    public List<Student> selectStudentMh(@RequestParam String name){
        QueryWrapper<Student> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("sname",name);
        List<Student> students=studentService.list(queryWrapper);
        return students;
    }
    //分页查询学生信息
    @RequestMapping("/selectStudentPage")
    public Page<Student> selectStudentPage(){
        Page<Student> page=new Page<>(1,1);
        studentService.page(page,null);
        page.getRecords().forEach(System.out::println);
        return page;
    }
    //添加学生信息
    @RequestMapping("/addStudent")
    public Boolean addStudent(@RequestBody Student student){
        return studentService.save(student);
    }
    //修改学生信息
    @RequestMapping("/updateStudent")
    public Boolean updateStudent(@RequestBody Student student){
        return studentService.updateById(student);
    }
    //根据ID删除学生
    @RequestMapping("/deleteStudentById")
    public Boolean deleteStudentById(@RequestParam Long id){
        return studentService.removeById(id);
    }
    //8）批量删除学生
    @RequestMapping("/deleteStudentByIds")
    public boolean deleteStudentById(@RequestBody List<Integer> ids) {
        return studentService.removeByIds(ids);
    }

}
